package com.ylq.controller;

import com.ylq.entity.Modules;
import com.ylq.entity.Result;
import com.ylq.service.ModulesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @Author qiang
 * @Description TODO
 * @Date 2020-04-30
 **/
@RestController
@Api(description = "【模块】相关的增删改查操作")
@RequestMapping("/api")
public class ModulesController {
    @Autowired
    private ModulesService modService;
    Modules modules = new Modules();
    @ApiOperation("创建模块")
    @PostMapping("CreateModule")
    public Object createModule(HttpSession session,@ApiParam("数据库密钥")@RequestParam("token") String token,@ApiParam("模块名称")@RequestParam("name") String name,@ApiParam("父模块id")@RequestParam("parentId") int parentId,@ApiParam("模块对应的路径")@RequestParam(value = "path",required=false) String path,@ApiParam("权重整数")@RequestParam(value = "weight",required=false) int weight){
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
        Modules modules = new Modules();
        modules.setTitle(name);
        modules.setParentId(parentId);
        modules.setPath(path);
        modules.setWeight(weight);
       int i = modService.createModule(modules);
        if (i>0){
            return new Result(modules.getTitle(),"",true);
        }else {
            return new Result("添加失败","",false);
        }

        }else {
            return new Result("", "数据库密钥不匹配", false);
        }
    }
    @ApiOperation("更新模块")
    @PostMapping("UpdateModule")
    public Object updateModule(HttpSession session,@ApiParam("数据库密钥")@RequestParam("token") String token,@ApiParam("模块id")@RequestParam("mld") int mld,@ApiParam("模块名称")@RequestParam("name") String name,@ApiParam("父模块id")@RequestParam("parentId") int parentId,@ApiParam("模块对应的路径")@RequestParam("path") String path,@ApiParam("权重整数")@RequestParam("weight") int weight){
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
        modules.setId(mld);
        modules.setTitle(name);
        modules.setParentId(parentId);
        modules.setPath(path);
        modules.setWeight(weight);
       int i = modService.updateModule(modules);
        if (i>0){
            return new Result("","修改成功",true);
        }else {
            return new Result("","修改失败",false);
        }

        }else {
            return new Result("", "数据库密钥不匹配", false);
        }
    }
    @ApiOperation("删除模块")
    @PostMapping("DeleteModule")
    public Object deleteModule(HttpSession session,@ApiParam("数据库密钥")@RequestParam("token") String token,@ApiParam("模块id")@RequestParam("mId") int mId){
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
            int i = modService.deleteModule(mId);
            modService.deleteMo(mId);
            if (i>0){
                return new Result("","删除成功",true);
            }else{
                return new Result("","删除失败",false);
            }
        }else {
            return new Result("", "数据库密钥不匹配", false);
        }

    }
    //加载树
    @ApiOperation("获取所有模块信息")
    @PostMapping("/GetAllModuleLayUI")
    public Object getTree(@ApiParam("数据库秘钥") @RequestParam("token")String token,HttpSession session) {
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
            return modService.loadTree();
        }else {
            return new Result("", "数据库密钥不匹配", false);
        }
        }

    /**
     * 获取权限
     */
    @ApiOperation("根据角色获取权限功能")
    @PostMapping("GetSysRightsJsonLayUI")
    public Object getSysRightLayui(@ApiParam("数据库秘钥") @RequestParam("token")String token,HttpSession session,
                                   @ApiParam("角色名称")@RequestParam(value = "roleNames")String[] roleNames) {
        String getToken = (String) session.getAttribute("token");
        if(getToken.equals(token)){
            String str[] = {"1","2","3"};
            List<String> strings = new ArrayList<String>(roleNames.length);
            Collections.addAll(strings,roleNames);
            //strings.add("4");
            System.out.println(strings);
            /*String[] array = new String[roleNames.size()];
            for (int i= 0; i<roleNames.size();i++){
                array[i] = roleNames.get(i);
            }
            System.out.println(StringUtils.join(array));*/
            return modService.getsysRight(strings);
        }else {
            return new Result("", "数据库密钥不匹配", false);
        }
    }

}
